<?php
/**
 * Medical Center (in a Medical Study) Main Screen
 * This screen displays the documents associated and the patients
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter
 * @license
 */
use Nawa\MedicalCenter;
use Fox\Html\Input;
use Fox\Html\Block;
use Fox\Generator;
use Fox\Html\Screen;

$xview = Xview::instance();
$msid = $xview->val('msid');
$mcid = $xview->val('mcid');
$keys = array('msid' => $msid,'mcid' => $mcid);
$ms = $xview->component('medicalstudies','object',$keys);

$nbCenters = $ms->countMedicalCentersPartners();
$centerPartner = $ms->findMedicalCentersPartners($msid.':'.$mcid);
$medicalCenter = $centerPartner->medicalCenter();

$data = array_merge($centerPartner->getData(),$medicalCenter->getData());
// Load Partners Data 
$data['gPartners'] = $centerPartner->partnersData();
// Load Patient Data
$data['gPatients'] = $centerPartner->patients();
// Load Monitorings Data
$data['gMonitorings'] = $centerPartner->monitorings();

$screen = new Screen("sCenter","update");

// Bread Crumb
$crumbs = Xview::instance()
	->component('medicalstudies','breadcrumb',
	array('deepness' => 3,
		  'customerid' => $ms->customer()->getId(),
		  'customer_name' => $ms->customer()->name1,
		  'msReference' => $ms->reference,
		  'msid' => $ms->getId(),
		  'msCenterCount' => $nbCenters,
		  'mcid' => $mcid,
		  'mcLabel' => $medicalCenter->label(),
		  'mcPatientCount' => count($data['gPatients'])
	)
);
$screen->breadcrumb()->crumbs($crumbs);
// Medical Center Information
$blHead = $xview->component('medicalcenters','head');
$blHead->item('reference')->hidden();
$blHead->add(new Input(array('mc_reference','reference_extern')));
$screen->add($blHead);
// Address Data
$blAddress = Xview::instance()->component('customers', 'address');
$blAddress->collapsed();
$screen->add($blAddress);
// Medical Center Partners
$bPartners = $xview->module(Generator::url('medicalstudies','medicalcenters', 'partners',$keys));
$screen->add($bPartners);

// Medical Center Patients
$bPatients = $xview->module(Generator::url('medicalstudies','medicalcenters', 'patients',$keys));
$screen->add($bPatients);

// Medical Center Documents
$genDoc = Generator::url('medicalstudies','medicalcenters', 'documents',$keys);
/* @var $gDocuments \Fox\Grid */
$gDocuments = $xview->module($genDoc);
$bDocuments = new Block('bDocuments',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::DOCUMENTS));
$bDocuments->add($gDocuments);
$bDocuments->collapsed(false);

// Monitorings
$gMonitorings = $xview->module(Generator::url('medicalstudies','medicalcenters', 'monitorings',$keys));
$bMonitorings = new Block('bMonitorings',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::MONITORINGS));
$bMonitorings->add($gMonitorings);
$screen->add($bMonitorings);

$screen->initialize($data);
if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {

			// Update Medical Center Address Information
			$addressData = $blAddress->get();
			$addressData['name1'] = $fields['name1'];
			$addressData['type'] = $fields['type'];
			$medicalCenter->setFields($addressData);
			$medicalCenter->prepareUpdate(MedicalCenter::CHANGE_DEFAULT);
			$medicalCenter->update();
			
			// Update Assign User List
			foreach($data['gPartners'] as $partnerData) {
				$existingPartner[$partnerData['customerid']] = $partnerData;
			}
			foreach($fields['gPartners'] as $partnerData) {
				if(isset($existingPartner[$partnerData['customerid']])) {
					unset($existingPartner[$partnerData['customerid']]);
				}
			}
			if(is_array($existingPartner) && count($existingPartner) > 0) {
				// Delete not posted partners
				foreach($existingPartner as $partnerData) {
					try {
						$inlineId = $msid.':'.$mcid.':'.$partnerData['customerid'];
						$partner = $centerPartner->findPartners($inlineId);
						$centerPartner->deleteFromPartners($partner);
					}
					catch(Fox\Object\Exception\NotExist $e) {
						
					}
				}
			}
			// Update Medical Center Information at Medical Study Level
			$centerPartner->setFields(array('reference_extern' => $fields['reference_extern']));
			$centerPartner->update();
			$screen->redirect();
		}
		catch (Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e);
		}
	}
}
echo $screen->display();